package uoc.biblioteca.mappings.dao;

// Generated 11/04/2011 14:10:24 by Hibernate Tools 3.2.1.GA

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import uoc.biblioteca.mappings.object.LlibreVo;
import uoc.biblioteca.mappings.object.ProveedorVo;
import uoc.biblioteca.sessionFactory.HibernateSessionFactory;

/**
 * Home object for domain model class Proveedor.
 * @see uoc.biblioteca.mappings.dao.ProveedorVo
 * @author Hibernate Tools
 */
public class ProveedorHome extends BaseHome {

	private static final Log log = LogFactory.getLog(ProveedorHome.class);

	private final SessionFactory sessionFactory = null;


	public ProveedorVo findById(java.lang.Integer id) {
		log.debug("getting ProveedorVo instance with id: " + id);
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			ProveedorVo instance = (ProveedorVo) session.get("uoc.biblioteca.mappings.object.ProveedorVo", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
				
			}
			
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void saveNew(ProveedorVo instance) {
		
		log.debug("save new ProveedorVo instance");
		
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			session.clear();
			
			tx = session.beginTransaction();
			
			session.saveOrUpdate(instance);
			
			tx.commit();
			session.close();
			
			log.debug("saveNew successful");
			
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	//Mostrar todos los proveedores
	public ProveedorVo[] getAllProveedores() {
		
		/* will hold the books we are going to return later */
		try{
			List proveedores = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.ProveedorVo as c order by c.nom ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				ProveedorVo proveedoresTemp = (ProveedorVo) iter.next();
				proveedores.add(proveedoresTemp);
			}
			tx.commit();
			
			return (ProveedorVo[]) proveedores.toArray(new ProveedorVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//Mostrar todos los proveedores con IsExport==true
	public ProveedorVo[] getAllProveedoresExportables() {
		
		/* will hold the books we are going to return later */
		try{
			List proveedores = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.ProveedorVo as c where c.isExport=1 order by c.nom ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				ProveedorVo proveedoresTemp = (ProveedorVo) iter.next();
				proveedores.add(proveedoresTemp);
			}
			tx.commit();
			
			return (ProveedorVo[]) proveedores.toArray(new ProveedorVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//Obtener el proveedor por codigo
	public ProveedorVo findProveedorByCode(String code) {
		
		/* will hold the books we are going to return later */
		try{
			ProveedorVo proveedor = new ProveedorVo();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.ProveedorVo as c where c.codeProveedor = '"+code+"'  ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				proveedor= (ProveedorVo) iter.next();
				
			}
			tx.commit();
			
			return proveedor;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


}
